<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="application"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加</title>
<link rel="stylesheet" href="${ctx}/static/js/layui/css/layui.css">
<style type="text/css">
	.msg{
		color : red !important;
	}
</style>
</head>
<body>
	<div style="padding: 10px;">
		<form class="layui-form" action="">
			<div class="layui-form-item">
				<label class="layui-form-label">父菜单</label>
				<div class="layui-input-block">
					<input type="text" name="pid" lay-filter="pid" id="pid"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">菜单名称</label>
				<div class="layui-input-inline">
					<input type="text" name="menuname" required
						lay-verify="required" placeholder="请输入菜单名称" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">菜单类型</label>
				<div class="layui-input-block">
					<select name="menutype" lay-verify="">
						<option value="菜单" selected="selected">菜单</option>
						<option value="按钮">按钮</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">图标</label>
				<div class="layui-input-inline">
					<input type="text" name="icon" required lay-verify="required"
						placeholder="请输入图标" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">权限标识符</label>
				<div class="layui-input-inline">
					<input type="text" name="permission" required
						lay-verify="required" placeholder="请输入权限标识符" autocomplete="off"
						class="layui-input">
				</div>
				<div class="layui-form-mid layui-word-aux"></div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">跳转地址</label>
				<div class="layui-input-block">
					<input type="text" name="url" required
						lay-verify="required" placeholder="请输入跳转地址" autocomplete="off"
						class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">回调方法</label>
				<div class="layui-input-block">
					<input type="text" name="method" required lay-verify="required"
						placeholder="请输入回调方法" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">序号</label>
				<div class="layui-input-block">
					<input type="text" name="seq" required lay-verify="required"
						placeholder="请输入序号" autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
		</div>
		<script src="${ctx}/static/js/layui/layui.js"></script>
		<script src="${ctx}/static/js/common.js"></script>
		<script>
		//Demo
			layui.config({
				base : '${ctx}/static/js/',
			})
			layui.use(['treeSelect', 'form', 'upload' ], function() {
				var form = layui.form;
				var upload = layui.upload;
				var treeSelect= layui.treeSelect;
				var $ = layui.$;

				//监听提交
				form.on('submit(formDemo)', function(data) {
					//$.post一般来说需要3个参数：url，参数，回调函数
					$.post('${ctx}/menu/add', data.field , function (e){
						if (e == "") {
							$('#refresh', parent.document).click();		//调用父页面（menu_list.jsp）中的id为refresh的元素的点击方法
							parent.layer.msg('添加成功', {icon: 1});		//高用父窗口中的layer来提示，这样关闭弹出窗后提示不会立马关闭
							var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
							parent.layer.close(index); //再执行关闭 
						} else {
							alert("失败");
						}
					})
					return false;
				});

				//光标离开后，去查询用户名是否存在
				$('input[name="username"]').blur(function() {
					var v = $(this).val();
					//Ajax请求
					$.ajax({
						url : '${ctx}/user/isExist', //地址
						data : { //data是参数
							username : v
						},
						success : function(e) { //成功后的回调函数
							if (e == "") {
								$('.msg').text("");
							} else {
								$('.msg').text(e);
								$('input[name="username"]').focus();
							}
						},
						error : function(xhr, status, error) { //失败后的回调函数
							console.log("失败：" + xhr);
						}
					})
				})
			
				//父级菜单树形下拉框
		        treeSelect.render({
		            elem: '#pid',
		            data: '${ctx}/menu/getParentMenu',
		            type: 'get',
		            placeholder: '请选择父级菜单',
		            search: true,
		            click: function(d){
		               	$('#pid').val(d.current.id);
		            },
		            success: function (d) {
		            	//选中下拉
		            	treeSelect.checkNode('pid', '${param.pid}');
		            	$('#pid').val('${param.pid}');
		            }
		        });
				 
				//上传头像
				var uploadInst = upload.render({
					elem : '#test1' //绑定元素
					,
					url : '${ctx}/upload' //上传接口
					,
					before : function(obj) {
						//预读本地文件示例，不支持ie8
						obj.preview(function(index, file, result) {
							$('#demo1').attr('src', result); //图片链接（base64）
						});
					},
					done : function(res) {
						//上传完毕回调
						console.log(res);
						$('#headImg').val(res.name);
					},
					error : function(e) {
						//请求异常回调
						console.log(e);
					}
				});
			});
		</script>
	</div>
</body>
</html>